<script setup lang="ts">
import { loginByCode, loginByPassword } from '@/services/user'
import { useStore } from '@/stores'
import { codedRules, mobileRules, passwordRules } from '@/utils/validate'
import { showSuccessToast, showToast } from 'vant'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useCountDown } from '@/composables'
const mobile = ref('')
const password = ref('')
const isAgree = ref(false)
const { user } = useStore()
const router = useRouter() // 等价于  this.$router  路由实例
const route = useRoute() // 等价于 this.$route 路由信息对象
const { fromRef, time, getCode } = useCountDown(mobile, 'login')
// 登录
const login = async () => {
  // 1. 协议许可勾选
  if (!isAgree.value) return showToast('请同意用户协议')
  // 2. 发送请求
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByCode(mobile.value, code.value)
  // 3. 存储用户信息
  user.setUser(res.data)
  // 4. 提示
  showSuccessToast('登录成功')
  // 5. 跳转
  route.query.returnUrl
    ? router.push(route.query.returnUrl as string)
    : router.push('/user')
  /* if (route.query.returnUrl) {
    router.push(route.query.returnUrl as string)
  } else {
    router.push('/user')
  } */
}

// 面板切换
const isPass = ref(true)

const code = ref('')

const isShow = ref(false)
//  import.meta.env.VITE_APP_CALLBACK === http://consult-patients.itheima.net    http%3A%2F%2Fconsult-patients.itheima.net

// decodeURIComponent(http://consult-patients.itheima.net)   encodeURIComponent()
const url = `https://graph.qq.com/oauth2.0/authorize?client_id=102015968&response_type=token&scope=all&redirect_uri=${encodeURIComponent(
  import.meta.env.VITE_APP_CALLBACK
)}%2Flogin%2Fcallback`
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="login" ref="fromRef">
      <van-field
        placeholder="请输入手机号"
        type="tel"
        v-model="mobile"
        :rules="mobileRules"
        name="mobile"
      ></van-field>
      <!-- 密码输入框 -->
      <van-field
        v-if="isPass"
        placeholder="请输入密码"
        :type="isShow ? 'text' : 'password'"
        v-model="password"
        :rules="passwordRules"
      >
        <template #button>
          <!-- <cp-icon :name="`login-eye-${isShow ? 'on' : 'off'}`" @click="isShow = !isShow"></cp-icon> -->
          <cp-icon
            name="login-eye-on"
            v-if="isShow"
            @click="isShow = !isShow"
          ></cp-icon>
          <cp-icon
            name="login-eye-off"
            v-else
            @click="isShow = !isShow"
          ></cp-icon>
        </template>
      </van-field>
      <!-- 短信验证码输入框 -->
      <van-field
        v-else
        center
        :rules="codedRules"
        clearable
        placeholder="请输入短信验证码"
        v-model="code"
      >
        <template #button>
          <span
            size="small"
            type="primary"
            :class="{ active: time }"
            class="btn-send"
            @click="getCode"
            >{{ time ? `${time}s后发送` : '发送验证码' }}</span
          >
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <a :href="url">
          <img src="@/assets/qq.svg" alt="" />
        </a>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
@import '@/styles/login.scss';
</style>
